package com.koocloud.electroplatemanage.service.impl;

import cn.hutool.core.lang.Validator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koocloud.electroplatemanage.common.pojo.ResponseTemplate;
import com.koocloud.electroplatemanage.mapper.AnalysisItemsMapper;
import com.koocloud.electroplatemanage.mapper.OperateMapper;
import com.koocloud.electroplatemanage.mapper.OriginItemsMapper;
import com.koocloud.electroplatemanage.pojo.*;
import com.koocloud.electroplatemanage.pojo.Operate;
import com.koocloud.electroplatemanage.service.OperateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.koocloud.electroplatemanage.constant.IncomePartJudgeSqlFieldConstant.*;
import static com.koocloud.electroplatemanage.constant.IncomePartJudgeSqlFieldConstant.ZERO;

/**
 * @program:
 * @description:
 * @author: wjx
 * @create: 2020/11/18 17:35
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OperateServiceImpl implements OperateService {

    @Resource
    OperateMapper operateMapper;

    @Resource
    OriginItemsMapper originItemsMapper;

    @Resource
    AnalysisItemsMapper analysisItemsMapper;
    Lock lock = new ReentrantLock();


    /**
     *@Description:通过主键查询一套数据
     *@Param: pk
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/19
     */
    @Override
    public ResponseTemplate selectByPk(String pk) {
        try {
            //新建工种实体
            Operate operate = null;
            operate = operateMapper.selectByPk(pk);
            //判断查表操作是否成功
            if (null != operate) {
                List<OriginItems> list = originItemsMapper.selectByTankNo(operate.getTankNo());
                List<AnalysisItems> analysisItemsList = analysisItemsMapper.selectByOperateId(operate.getOperateID());
                operate.setAnalysisItemsList(analysisItemsList);
                if (!list.isEmpty()){
                    operate.setOriginItemsList(list);
                    //查表成功返回数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count(1l).data(operate).build();
                }else {
                    //查表成功返回数据
                    return ResponseTemplate.builder().code(0).message("该镀槽没有化验项目").count(1l).data(operate).build();
                }
            }
            else {
                //查表失败返回提示
                return ResponseTemplate.builder().code(1).message("没有此数据").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }
    }

    /**
     *@Description:查询所有数据分页
     *@Param: [page,limit,operate]
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/19
     */
    @Override
    public ResponseTemplate selectAllData(String page, String limit, Operate operate) {
        //校验分页参数是否为纯数字 并且page和limit不都等于0
        if(Validator.isNumber(page)&&Validator.isNumber(limit)&&!("0".equals(page)&&"0".equals(limit))) {
            try {
                //开始分页
                PageHelper.startPage(Integer.valueOf(page), Integer.valueOf(limit));
                List<Operate> OperateList = new ArrayList<>();
                //判断模糊查询是否为状态查询 启用为 1 禁用为 0
                if (!StringUtils.isEmpty(operate.getIdOrName())){
                    //启用
                    if (STARTUSING.equals(operate.getIdOrName())){
                        operate.setIdOrName(ONE);
                        OperateList = operateMapper.selectByDeleteFlag(operate);
                        //禁用
                    } else if (FORBIDDEN.equals(operate.getIdOrName())){
                        operate.setIdOrName(ZERO);
                        OperateList = operateMapper.selectByDeleteFlag(operate);
                        //模糊查询
                    }else {
                        OperateList = operateMapper.selectAllData(operate);
                    }
                }else {
                    //全局查询
                    OperateList = operateMapper.selectAllData(operate);
                }
                //构造分页实体
                PageInfo<Operate> info=new PageInfo<>(OperateList);
                if (info.getTotal()>0) {
                    //查询到数据
                    return ResponseTemplate.builder().code(0).message("查询成功").count(info.getTotal()).data(info.getList()).build();
                } else {
                    //没有查询到数据
                    return ResponseTemplate.builder().code(1).message("没有查到对应数据").count(0l).build();
                }
            } catch (Exception e) {
                //遇到异常，返回异常原因
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
            }
        }
        else {
            return ResponseTemplate.builder().code(1).message("分页参数错误").count(0l).build();
        }
    }

    @Override
    public ResponseTemplate check(Operate operate) {
        return null;
    }

    /**
     *@Description:插入一条数据
     *@Param: operate
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/19
     */
    @Override
    public ResponseTemplate insert(Operate operate) {
        try {
            lock.lock();
            int rows = operateMapper.insert(operate);
            //analysisItemsMapper.insert()
            //判断删除操作是否成功
            if (rows > 0) {
                //删除成功返回数据
                return ResponseTemplate.builder().code(0).message("插入成功").count(1l).build();
            }
            else {
                //删除失败返回提示
                return ResponseTemplate.builder().code(1).message("插入失败").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }

    @Override
    public ResponseTemplate batchInsert(List<Operate> listOperate) {
        return null;
    }

    /**
     *@Description:删除一条数据
     *@Param: operate
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/19
     */
    @Override
    public ResponseTemplate delete(String pk, String deleteFlag) {
        try {
            lock.lock();
            int rows = operateMapper.delete(pk,deleteFlag);
            //判断删除操作是否成功
            if (rows > 0) {
                //删除成功返回数据
                return ResponseTemplate.builder().code(0).message("删除成功").count(1l).build();
            }
            else {
                //删除失败返回提示
                return ResponseTemplate.builder().code(1).message("删除失败").count(0l).build();
            }
        } catch (Exception e) {
            //遇到异常，返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }

    @Override
    public ResponseTemplate batchDeleteByPks(List<String> listPks) {
        return null;
    }

    /**
     *@Description:更新一条数据
     *@Param: operate
     *@return: com.koocloud.electroplatemanage.common.ResponseTemplate
     *@Author: wjx
     *@date: 2020/11/19
     */
    @Override
    public ResponseTemplate update(Operate operate) {
        try {
            lock.lock();
            //没有异常并且修改数据条数大于0
            int rows = operateMapper.update(operate);
            if (rows > 0) {
                return ResponseTemplate.builder().code(0).message("修改数据成功").count(1l).build();
            } else {//没有异常并且修改数据失败，原则上不出现
                return ResponseTemplate.builder().code(1).message("修改数据失败").count(0l).build();
            }
        } catch (Exception e) {
            //出现异常返回异常原因
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseTemplate.builder().code(1).message(e.getMessage()).count(0l).build();
        }finally {
            lock.unlock();
        }
    }
}
